<?php

/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Sitealbum
 * @copyright  Copyright 2010-2011 BigStep Technologies Pvt. Ltd.
 * @license    http://www.socialengineaddons.com/license/
 * @version    $Id: Controller.php 2011-08-026 9:40:21Z SocialEngineAddOns $
 * @author     SocialEngineAddOns
 */
class Sitealbum_Widget_ListAlbumsTabsViewController extends Engine_Content_Widget_Abstract {

  public function indexAction() {
    $this->view->is_ajax = $is_ajax = $this->_getParam('isajax', '');
    $this->view->showViewMore = $this->_getParam('showViewMore', 1);
    if (empty($is_ajax)) {
      $this->view->tabs = $tabs = Engine_Api::_()->getItemTable('socialengineaddon_tab')->getTabs(array('module' => 'sitealbum', 'type' => 'albums', 'enabled' => 1));
      $count_tabs = count($tabs);
      if (empty($count_tabs)) {
        return $this->setNoRender();
      }
      $activeTabName = $tabs[0]['name'];
    }
    if (!Engine_Api::_()->getApi('settings', 'core')->getSetting('sitealbum.viewerphoto', 0)) {
      return $this->setNoRender();
    }

    $getLightBox = Zend_Registry::isRegistered('sitealbum_getlightbox') ? Zend_Registry::get('sitealbum_getlightbox') : null;
    if (empty($getLightBox)) {
      return;
    }
    $this->view->marginPhoto = $this->_getParam('margin_photo', 12);
    $table = Engine_Api::_()->getItemTable('album');
    $select = $table->select()
            ->where('search = ?', true);

    $paramTabName = $this->_getParam('tabName', '');

    if (!empty($paramTabName))
      $activeTabName = $paramTabName;

    $activeTab = Engine_Api::_()->getItemTable('socialengineaddon_tab')->getTabs(array('module' => 'sitealbum', 'type' => 'albums', 'enabled' => 1, 'name' => $activeTabName));
    $this->view->activTab = $activTab = $activeTab['0'];

    switch ($activTab->name) {
      case 'recent_albums':
        break;
      case 'liked_albums':
        $select->order('like_count DESC');
        break;
      case 'viewed_albums':
        $select->order('view_count DESC');
        break;
      case 'commented_albums':
        $select->order('comment_count DESC');
        break;
      case 'featured_albums':
        $select->where('featured = ?', 1);
        $select->order('Rand()');
        break;
      case 'random_albums':
        $select->order('Rand()');
        break;
    }

    if ($activTab->name != 'featured_albums' && $activTab->name != 'random_albums') {
      $select->order('creation_date DESC');
      if (!Engine_Api::_()->sitealbum()->canShowSpecialAlbum())
        $select->where('type IS NULL');
    }
    
    $this->view->paginator = $paginator = Zend_Paginator::factory($select);
    $paginator->setItemCountPerPage($activTab->limit);
    $paginator->setCurrentPageNumber($this->_getParam('page', 1));
    $this->view->count = $paginator->getTotalItemCount();
    $this->view->canCreate = Engine_Api::_()->authorization()->isAllowed('album', null, 'create');
    $viewerType = Engine_Api::_()->getApi('settings', 'core')->getSetting('sitealbum.viewertype', null);
    if (empty($viewerType)) {
      return $this->setNoRender();
    }
  }

}

?>
